package com.nuaa.ange.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.nuaa.ange.annotation.ApiLog;
import com.nuaa.ange.common.lang.Consts;
import com.nuaa.ange.common.lang.RespBean;
import com.nuaa.ange.entity.Reply;
import com.nuaa.ange.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

/**
 * @description
 * @author 玦离
 * @date 2023/1/2 21:17
 */

@RestController
@Api(tags = "评论相关接口")
@RequestMapping("/reply")
public class ReplyController extends BaseController{

    @ApiOperation(value = "重新编辑文章文章")
    @PostMapping("/update")
    @ApiLog
    public RespBean updateArticle(@RequestBody Reply reply) {
        User author = userService.getById(getProfileId());
        if (author.getUserState()) {
            replyService.update(reply, new UpdateWrapper<Reply>()
                    .eq("reply_id", reply.getReplyId()));
            return RespBean.ok("文章已重新编辑");
        }
        return RespBean.error("您已被禁言，无法重新编辑");
    }

    @ApiOperation(value = "删除回复")
    @GetMapping("delete")
    public RespBean deleteReply(@RequestParam("replyId") int replyId) {
        replyService.removeById(replyId);
        return RespBean.ok("回复已成功删除");
    }

    @ApiOperation(value = "举报评论")
    @GetMapping("/reported")
    @ApiLog
    public RespBean updateReplyReported(@RequestParam("replyId") int replyId) {
        replyService.update(new UpdateWrapper<Reply>()
                .eq("reply_id", replyId)
                .setSql("'reply_reported_num' = 'reply_reported_num' + 1"));
        Reply reply = replyService.getById(replyId);
        // 举报次数 < 3
        if (reply.getReplyReportedNum() < Consts.REPORTED_NUM) {
            return RespBean.ok("举报成功");
        }
        // 将状态设为 0 ，即被举报
        reply.setReplyState(false);
        // 举报次数 ≥ 3
        return RespBean.ok("帖子被禁");
    }
}
